// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_MST_PERSON_CUSTOMER_HISTORY
// * CreateDate    : 2011/11/21 10:37:36
// * Creater       : CodeSmith
// * LastChangeDate: 2011/11/21 10:37:36
// * LastChanger   : 2011/11/21 10:37:36
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Mst
{
/// <summary>
    /// MstPersonCustomerHistory
    /// </summary>
    public partial class MstPersonCustomerHistoryBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objMstPersonCustomerHistoryEntity">Entity</param>
        /// <returns>Entity</returns>
        public static MstPersonCustomerHistoryEntity GetMstPersonCustomerHistory(MstPersonCustomerHistoryEntity objMstPersonCustomerHistoryEntity)
        {
            return DataAccess.SelectSingle<MstPersonCustomerHistoryEntity>(objMstPersonCustomerHistoryEntity);
        }
        public static MstPersonCustomerHistoryEntity GetById(string historyId)
        {
            if (string.IsNullOrEmpty(historyId))
            {
                return null;
            }
            MstPersonCustomerHistoryEntity entity = new MstPersonCustomerHistoryEntity();
            entity.HistoryId = historyId;
            return DataAccess.SelectSingle<MstPersonCustomerHistoryEntity>(entity);
        }
        public static MstPersonCustomerHistoryEntity GetCurrencyPersonCustomer(string customerId)
        {
            if (string.IsNullOrEmpty(customerId))
            {
                return null;
            }
            string sql = "SELECT TOP 1 * FROM MST_PERSON_CUSTOMER_HISTORY WHERE CUSTOMER_ID = '" + customerId + "' ORDER BY LAST_MODIFIED_DATE DESC";
            List<MstPersonCustomerHistoryEntity> list = DataAccess.Select<MstPersonCustomerHistoryEntity>(sql);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            return null;
        }
        public static MstPersonCustomerHistoryEntity GetPersonCustomerByDate(string customerId, DateTime startDate)
        {
            if (string.IsNullOrEmpty(customerId))
            {
                return null;
            }
            string sql = @"SELECT * FROM MST_PERSON_CUSTOMER_HISTORY 
                            WHERE CUSTOMER_ID = '" + customerId + @"' 
	                            AND FROM_DATE <= CONVERT(DATETIME,'" + startDate.ToString("yyyy-MM-dd") + @" 23:59:59')
                            ORDER BY LAST_MODIFIED_DATE DESC";
            List<MstPersonCustomerHistoryEntity> list = DataAccess.Select<MstPersonCustomerHistoryEntity>(sql);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            return null;
        }
        public static DataTable GetCustomerPersonHistory(string customerId)
        {
            string sql = @"SELECT *, C.CUSTOMER_NAME, P.NAME_CN
                            FROM MST_PERSON_CUSTOMER_HISTORY PCH
                            INNER JOIN MST_CUSTOMER C ON C.CUSTOMER_ID = PCH.CUSTOMER_ID
                            INNER JOIN MST_PERSON P ON P.PERSON_ID = PCH.PERSON_ID
                            WHERE PCH.CUSTOMER_ID = '" + customerId + @"'
                            ORDER BY PCH.FROM_DATE DESC";
            return DataAccess.SelectDataSet(sql).Tables[0];
        }
        #endregion

        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objMstPersonCustomerHistoryEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertMstPersonCustomerHistory(MstPersonCustomerHistoryEntity objMstPersonCustomerHistoryEntity)
        {
            return DataAccess.Insert(objMstPersonCustomerHistoryEntity);
        }

        public static bool InsertMstPersonCustomerHistory(List<MstPersonCustomerHistoryEntity> objMstPersonCustomerHistoryEntityList)
        {
            return DataAccess.Insert(objMstPersonCustomerHistoryEntityList);
        }
        #endregion

        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objMstPersonCustomerHistoryEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateMstPersonCustomerHistory(MstPersonCustomerHistoryEntity objMstPersonCustomerHistoryEntity)
        {
            return DataAccess.Update(objMstPersonCustomerHistoryEntity);
        }

        public static bool UpdateMstPersonCustomerHistory(List<MstPersonCustomerHistoryEntity> objMstPersonCustomerHistoryEntityList)
        {
            return DataAccess.Update<MstPersonCustomerHistoryEntity>(objMstPersonCustomerHistoryEntityList);
        }
        #endregion

        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objMstPersonCustomerHistoryEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteMstPersonCustomerHistory(MstPersonCustomerHistoryEntity objMstPersonCustomerHistoryEntity)
        {
            return DataAccess.Delete(objMstPersonCustomerHistoryEntity);
        }

        public static bool DeleteMstPersonCustomerHistory(List<MstPersonCustomerHistoryEntity> objMstPersonCustomerHistoryEntityList)
        {
            return DataAccess.Delete<MstPersonCustomerHistoryEntity>(objMstPersonCustomerHistoryEntityList);
        }
        #endregion

        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetMstPersonCustomerHistoryAll()
        {
            return DataAccess.SelectAllDataSet<MstPersonCustomerHistoryEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<MstPersonCustomerHistoryEntity> GetMstPersonCustomerHistoryAllList()
        {
            return DataAccess.SelectAll<MstPersonCustomerHistoryEntity>();
        }
        #endregion
	}
	
	
}	

